package xj.graph2d.command;

import xj.graph2d.DrawAttr;
import xj.graph2d.DrawShape;
import xj.graph2d.SimpleDrawDocument;
import xj.graph2d.SimpleDrawView;

public class SetAttrCommand extends DeltaCommand {

  public SetAttrCommand(SimpleDrawView view, SimpleDrawDocument doc,
      DrawShape shape) {
    super(view, doc, shape);
    if (shape != null) {
      DrawAttr attr = shape.getDrawAttr();
      if (attr == null && view != null) {
	attr = view.getDrawAttr();
      }
      if (attr != null) {
	start = new DrawAttr();
	attr.copyDrawAttr(start);
      }
    }
  }

  @Override
  protected void forward() {
    DrawAttr attr = shape.getDrawAttr();
    if (attr == null && view != null) {
      attr = view.getDrawAttr();
    }
    if (attr != null && end != null) {
      end.copyDrawAttr(attr);
      shape.calculateBounds();
    }
  }

  @Override
  protected void backward() {
    DrawAttr attr = shape.getDrawAttr();
    if (attr == null && view != null) {
      attr = view.getDrawAttr();
    }
    if (end == null && attr != null) {
      end = new DrawAttr();
      attr.copyDrawAttr(end);
    }
    if (attr != null && start != null) {
      start.copyDrawAttr(attr);
      shape.calculateBounds();
    }
  }

  protected DrawAttr start = null;

  protected DrawAttr end = null;

}
